﻿<?php
// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);
//removes notices
error_reporting(E_ALL ^ E_NOTICE);
//add functions for checking files/directories
require_once('Functions/getFilesFromDir.php');
//add functions for retieving data from another site
require_once('Functions/Externjob.php');
//add function for connecting to database
require_once('Functions/db_connect.php');
//add function for triggering modules functions
require_once('Functions/Modules.php');
//add function for checking extern sites
require_once('Functions/Timers.php');
//add function to make a function from file in run-time
require_once('Functions/makeFunction.php');
//add functions for creating and handling the module-array
require_once('Functions/ModuleArray.php');
//connect to database
$link = con();

//settings
$server = "irc.quakenet.org";
$port = 6667;
$nick = "Jacqli";
$ctimer = 4;
$master = "penthero";
$mods = array();
$users = array();
date_default_timezone_set('Europe/Stockholm');
$dltimer = 2;
$maxmsg = 30;
$currmsg = 0;
$uptime = time();
$public_data = array(null,null,null,null,null,null,null, $uptime);
$sys_modules = sys_modules(getFilesFromDir('Modules/System'));
$priv_modules = priv_modules(getFilesFromDir('Modules'));

//get mod/admins
$result = mysql_query("SELECT nick FROM users WHERE `admin`=1 OR `mod`=1");
while($row = mysql_fetch_array($result)){
	$mods[] = $row[0];
} 
//setting up bot info on irc
$threads = array();
$threads[] = @JobStartAsync('localhost','/Jacqli-j1.php') or die("could not connect: $errstr ($errno)"); //connect to the script that checks news on different sites
echo "Threads started.\n";
//get the latest num of used gee's

main: $socket = @fsockopen($server, $port); //connect to the irc-server
if(!$socket){
	echo "trying to reconnect...\n";
	sleep(60);
	goto main;
}
echo "connected to irc.\n";
$uptime = time();
$nick = "Jacqli";
sleep(1);
fputs($socket,"USER $nick $nick $nick $nick :$nick\n");
fputs($socket,"NICK $nick\n");
flush();
$logincount = 0;

while($logincount < 10) {
	$logincount++;	
	// Separate all data
	echo $data;
	$ex = explode(' ', fgets($socket, 128));
	// Send PONG back to the server
	if($ex[0] == "PING"){
		fputs($socket, "PONG ".$ex[1]."\n");
	}
	flush();
}
sleep(1);
fputs($socket,"PRIVMSG Q@CServe.quakenet.org :AUTH Jacqli MnPd3uFYTc");
fgets($socket);
fputs($socket,"MODE $nick +i\n");
fputs($socket,"MODE $nick +x\n");
sleep(1);

$result = mysql_query("SELECT channel FROM channels");
while($row = mysql_fetch_array($result)){
	fputs($socket,"JOIN ".$row[0]."\n");
	echo "joined channel: ".trim($row[0])."\n";
	usleep(200000);
}
//fputs($socket,"JOIN #ondbot\n");
//set the timestamp to see if we disconnected or not
$disctimer = time();
//start inf-loop that checks input and timers
while(1) {	
	mysql_close($link);
	$link = con();	
	$data = extern($threads);
	foreach($data as &$data){
		$currmsg = msg($socket, "$data\n", $currmsg);
	}
	//stream_set_blocking($socket,false);
	if($data = fgets($socket)){
		$disctimer = time();
		$public_data = Priv($socket, $data, $public_data, $priv_modules, $sys_modules);
	}elseif(time() - $disctimer >= 360){
		goto main;
	}if($socket === false){
		goto main;
	}
	//usleep(500000);
}
?>